﻿@* @inject IProcessService ProcessService

<MudDialog>
    <DialogContent>
        <MudForm @ref="_editRequestForm" Model="_editRequestDto">
            <MudGrid>
                <MudItem xs="12" md="6">
                    <MudAutocomplete T="ProcessInfoResponseDto"
                    @bind-Value="_editRequestDto.Process" For="@(() => _editRequestDto.Process)"
                                     SearchFunc="@ProcessSearch" ToStringFunc="@(e => e?.Code)"
                                     Label="工序编码" Dense="true" Margin="Margin.Dense" Variant="Variant.Outlined" />
                </MudItem>
                <MudItem xs="12" md="6">
                    <MudTextField @bind-Value="_editRequestDto.Process.Name" Label="工序名称"
                                  Variant="Variant.Outlined" Margin="Margin.Dense" Disabled="true">
                    </MudTextField>
                </MudItem>
                <MudItem xs="12" md="6">
                    <MudAutocomplete T="ProcessInfoResponseDto"
                    @bind-Value="_editRequestDto.Process" For="@(() => _editRequestDto.Process)" 
                                     SearchFunc="@ProcessSearch" ToStringFunc="@(e => e?.Code)"
                                     Label="工序编码" Dense="true" Margin="Margin.Dense" Variant="Variant.Outlined" /> 
                </MudItem>
                <MudItem xs="12" md="6">
                    <MudTextField @bind-Value="_editRequestDto.Process.Name" Label="工序名称"
                                  Variant="Variant.Outlined" Margin="Margin.Dense" Disabled="true">
                    </MudTextField>
                </MudItem>
                <MudItem xs="12" md="6">
                    <MudNumericField T="int" @bind-Value="_editRequestDto.Quantity" For="@(() => _editRequestDto.Quantity)"
                                     Label="排产数量" Variant="Variant.Outlined" Margin="Margin.Dense" />
                </MudItem>
                <MudItem xs="12" md="6">
                    <MudDatePicker @bind-Date="_editRequestDto.StartTime" For="(() => _editRequestDto.StartTime)"
                                   Label="开始时间" AnchorOrigin="Origin.BottomCenter" DisableToolbar="true"
                                   Variant="Variant.Outlined" Margin="Margin.Dense">
                    </MudDatePicker>
                </MudItem>
                <MudItem xs="12" md="6">
                    <MudNumericField T="int" @bind-Value="_editRequestDto.TakeTime" For="@(() => _editRequestDto.TakeTime)"
                                     Label="生产时长" Variant="Variant.Outlined" Margin="Margin.Dense" />
                </MudItem>
                <MudItem xs="12" md="6">
                    <MudDatePicker @bind-Date="_editRequestDto.EndTime" For="(() => _editRequestDto.EndTime)"
                                   Label="结束时间" AnchorOrigin="Origin.BottomCenter" DisableToolbar="true"
                                   Variant="Variant.Outlined" Margin="Margin.Dense">
                    </MudDatePicker>
                </MudItem>
            </MudGrid>
        </MudForm>
    </DialogContent>
    <DialogActions>
        <MudButton Variant="Variant.Filled" Color="Color.Primary" OnClick="SubmitHandle">保存</MudButton>
        <MudButton Variant="Variant.Filled" OnClick="CancelHandle">取消</MudButton>
    </DialogActions>
</MudDialog>

@code {

    private ProductionSchedulingDetailEditRequestDto _editRequestDto;
    private MudForm _editRequestForm = new();

    [CascadingParameter]
    MudDialogInstance? MudDialog { get; set; }

    [Parameter]
    public ProductionSchedulingDetailEditRequestDto? Model { get; set; }

    private async Task<IEnumerable<ProcessInfoResponseDto>> ProcessSearch(string value)
    {
        var result = await ProcessService.QueryProcesses(new ProcessSearchRequestDto());
        if (result != null && result.Succeeded)
        {
            return result.Data!;
        }
        return new List<ProcessInfoResponseDto>();
    }

    protected override async Task OnInitializedAsync()
    {
        _editRequestDto = Model;
        await base.OnInitializedAsync();
    }

    void CancelHandle()
    {
        MudDialog?.Cancel();
    }

    void SubmitHandle()
    {
        MudDialog?.Close(DialogResult.Ok(_editRequestDto));
    }

} *@